﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace olierFrm.WebFormDev.Session
{
    public class SGrid
    {
        public const string SessionID = "SessionGrid";
        /// <summary>
        /// SessionGrid实体
        /// </summary>
        [Serializable]
        public class SessionGridEntity
        {
            private List<string> _QueryName = new List<string>();
            public List<string> QueryName
            {
                get { return _QueryName; }
                set { _QueryName = value; }
            }

            private Dictionary<string, List<string>> _Group = new Dictionary<string, List<string>>();
            public Dictionary<string, List<string>> Group
            {
                get { return _Group; }
                set { _Group = value; }
            }

            private Dictionary<string, string> _Sort = new Dictionary<string, string>();
            public Dictionary<string, string> Sort
            {
                get { return _Sort; }
                set { _Sort = value; }
            }
        }

        /// <summary>
        /// 初始化Session对象
        /// </summary>
        private static void initSession()
        {
            if (HttpContext.Current.Session[SessionID] == null)
            {
                HttpContext.Current.Session[SessionID] = new SessionGridEntity();
            }
        }

        /// <summary>
        /// 删除SessionGrid对象，包括QueryName、Group
        /// </summary>
        /// <param name="QueryName"></param>
        /// <returns></returns>
        public static int Clear(string QueryName)
        {
            if (HttpContext.Current.Session[SessionID] == null) return 0;

            var sge = (SessionGridEntity)HttpContext.Current.Session[SessionID];
            sge.Group.Remove(QueryName);
            sge.QueryName.Remove(QueryName);
            return 1;
        }

        /// <summary>
        /// 增加QueryName
        /// </summary>
        /// <param name="QueryName"></param>
        /// <returns>False代表不成功（QueryName存在），True代表成功增加。</returns>
        public static bool AddQueryName(string QueryName)
        {
            initSession();
            var sge = (SessionGridEntity)HttpContext.Current.Session[SessionID];
            if (sge.QueryName.Contains(QueryName))
                return false;
            else
                sge.QueryName.Add(QueryName);
            return true;
        }

        /// <summary>
        /// 是否存在QueryName
        /// </summary>
        /// <param name="QueryName"></param>
        /// <returns></returns>
        public static bool HaveQueryName(string QueryName)
        {
            initSession();
            var sge = (SessionGridEntity)HttpContext.Current.Session[SessionID];
            return sge.QueryName.Contains(QueryName);
        }

        /// <summary>
        /// 增加分组字段
        /// </summary>
        /// <param name="QueryName"></param>
        /// <param name="Fields"></param>
        /// <returns>返回增加字段的数量。</returns>
        public static int AddGroup(string QueryName, List<string> Fields)
        {
            initSession();
            var sge = (SessionGridEntity)HttpContext.Current.Session[SessionID];
            if (sge.Group.ContainsKey(QueryName))
            {
                int i = 0;
                foreach (var f in Fields)
                {
                    if (sge.Group[QueryName].Contains(f)) continue;

                    sge.Group[QueryName].Add(f);
                    i++;
                }
                return i;
            }
            else
            {
                sge.Group.Add(QueryName, Fields);
                return Fields.Count;
            }

        }

        /// <summary>
        /// 增加StdQuery的分组
        /// </summary>
        /// <param name="QueryName"></param>
        /// <param name="Field">字段</param>
        /// <returns>False代表不成功（字段存在分组里），True代表成功增加。</returns>
        public static bool AddGroup(string QueryName, string Field)
        {
            initSession();
            var sge = (SessionGridEntity)HttpContext.Current.Session[SessionID];
            if (sge.Group.ContainsKey(QueryName))
            {
                if (sge.Group[QueryName].Contains(Field)) return false;
                sge.Group[QueryName].Add(Field);
            }
            else
            {
                var list = new List<string>();
                list.Add(Field);
                sge.Group.Add(QueryName, list);
            }
            return true;
        }

        /// <summary>
        /// 删除StdQuery的分组
        /// </summary>
        /// <param name="QueryName"></param>
        /// <param name="Field">字段</param>
        /// <returns>False代表不成功（字段不存在分组里），True代表成功删除。</returns>
        public static bool RemoveGroup(string QueryName, string Field)
        {
            initSession();
            var sge = (SessionGridEntity)HttpContext.Current.Session[SessionID];
            if (sge.Group.ContainsKey(QueryName))
            {
                sge.Group[QueryName].Remove(Field);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取StdQuery的分组
        /// </summary>
        /// <param name="QueryName"></param>
        /// <returns>数组</returns>
        public static List<string> GetGroup(string QueryName)
        {
            initSession();
            var sge = (SessionGridEntity)HttpContext.Current.Session[SessionID];
            if (sge.Group.ContainsKey(QueryName))
            {
                return sge.Group[QueryName];
            }
            return null;
        }

        /*
        public static SessionGrid Session
        {
            get
            {
                if (HttpContext.Current.Session[SessionID] == null)
                {
                    SessionGrid.Session = new SessionGrid();
                    return null;
                }
                return (SessionGrid)System.Web.HttpContext.Current.Session[SessionID];
            }
            set
            {
                HttpContext.Current.Session[SessionID] = value;
            }
        }*/
        /*
       private List<string> _Grid = new List<string>();
       public List<string> Grid
       {
           get { return _Grid; }
           set { _Grid = value; }
       }
       
       public static void BindGridToSession(string QueryName, bool Clear = false)
       {
           if (Clear)
           {
               SessionGrid.Session.Grid.Remove(QueryName);
               BindGroupToSession(QueryName, Clear: true);
           }
           else if (QueryName != null)
           {
               SessionGrid.Session.Grid.Add(QueryName);
           }
       }

       public List<string> QueryName { get; set; }

       public static void BindGroupToSession(string QueryName, List<string> Groups = null, string AddField = null, string RemoveField = null, bool Clear = false)
       {

           if (Clear)
           {
               Session.Group.Remove(QueryName);
           }
           else if
           (!Session.Group.ContainsKey(QueryName)) Session.Group.Add(QueryName, new List<string>());
           if (Groups != null)
           {
               Groups.Remove("");
               Session.Group.Add(QueryName, Groups);
           }
           if (AddField != null && !Session.Group[QueryName].Contains(AddField))
               Session.Group[QueryName].Add(AddField);
           if (RemoveField != null)
           {
               Session.Group[QueryName].Remove(RemoveField);
           }
       }

       private Dictionary<string, DataTable> _DataSource = new Dictionary<string, DataTable>();
       public Dictionary<string, DataTable> DataSource
       {
           get { return _DataSource; }
           set { _DataSource = value; }
       }
       private Dictionary<string, List<string>> _Group = new Dictionary<string, List<string>>();
       public Dictionary<string, List<string>> Group
       {
           get { return _Group; }
           set { _Group = value; }
       }

       private Dictionary<string, string> _Sort = new Dictionary<string, string>();
       public Dictionary<string, string> Sort
       {
           get { return _Sort; }
           set { _Sort = value; }
       }
        */
    }
}
